<template>
    <a-modal
            :title="modalTitle"
            :width="800"
            :visible="visible"
            :confirmLoading="confirmLoading"
                    class="ant-modal2"
                    @cancel="handleCancel"
    >
        <a-form :form="form">

            <a-form-item v-show="false">
                <a-input v-decorator="['id']" type="hidden"/>
            </a-form-item>

                            <row-list :col="2">
                                                                                                                    <row-item>

                                <a-form-item
                                        label="总需求计划主键"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['demandPlanId', {rules: [{required: true, message: '总需求计划主键不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="采购计划主键"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['purchasePlanId', {rules: [{required: true, message: '采购计划主键不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="采购申请ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['purchaseApplyId', {rules: [{required: true, message: '采购申请ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="采购单ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['purchaseOrderId', {rules: [{required: true, message: '采购单ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="发运单ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['dispatchOrderId', {rules: [{required: true, message: '发运单ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="主清单ID"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['mainListId', {rules: [{required: true, message: '主清单ID不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="名称"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['name', {rules: [{required: true, message: '名称不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="规格"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['specs', {rules: [{required: true, message: '规格不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="数量"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                            <a-input-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['quantity', {rules: [{required: true, message: '数量不能为空'}]}]"/>
                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="单位"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                                <a-select
                                                v-decorator="['unit', {rules: [{required: true, message: '单位不能为空'}]}]"
                                                placeholder="请选择">
                                                                                        <a-select-option
                                                    v-for="(label,value) in unitMap"
                                                    :key="value"
                                                    :label="label"
                                                    :value="parseInt(value)">{{ label }}
                                            </a-select-option>
                                        </a-select>
                                                                    </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="单价"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                            <a-input-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['price', {rules: [{required: true, message: '单价不能为空'}]}]"/>
                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="总价"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                            <a-input-number
                                                style="width: 100%"
                                                :min="0"
                                                :step="0.01"
                                                :formatter="BaseTool.Amount.formatter"
                                                :parser="BaseTool.Amount.parser"
                                                v-decorator="['totalMoney', {rules: [{required: true, message: '总价不能为空'}]}]"/>
                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="备注"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                            <a-input
                                                v-decorator="['remark', {rules: [{required: true, message: '备注不能为空'}]}]"/>
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                <row-item>

                                <a-form-item
                                        label="删除标志"
                                        :labelCol="BaseTool.Constant.labelCol"
                                        :wrapperCol="BaseTool.Constant.wrapperCol"
                                >
                                                                                                                                                                                                                                                        </a-form-item>
                            </row-item>
                                                                                                                                                                                                                                                                                                                                    </row-list>
                    </a-form>
        <template slot="footer">
            <a-button :loading="confirmLoading" type="primary" @click="save()">保存</a-button>
        </template>
    </a-modal>
</template>

<script>
    import pick from 'lodash.pick'
    import {addPurchaseContractList, updatePurchaseContractList } from '@/api/purchase/purchase-contract-list'

    export default {
        name: 'BasePurchaseContractList',
        data() {
            return {
                confirmLoading: false,
                modalTitle: null,
                form: this.$form.createForm(this),
                visible: false,
            // 下拉框map
                            unitMap:{}
            ,
                    }
        },
        props: {},
        created() {
            // 下拉框map
                            this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_CONTRACT_LIST_UNIT)
                    },
        methods: {
            base(record) {
                this.visible = true
                // 如果是空标识添加
                if (this.BaseTool.Object.isBlank(record)) {
                    this.modalTitle = '添加'
                    return
                }
                this.modalTitle = '编辑'
                const {form: {setFieldsValue}} = this
                // 日期处理
                                this.$nextTick(() => {
                    setFieldsValue(Object.assign(pick(record, [
                                                                                    'id',
                                                                                                                'demandPlanId',
                                                                                                                'purchasePlanId',
                                                                                                                'purchaseApplyId',
                                                                                                                'purchaseOrderId',
                                                                                                                'dispatchOrderId',
                                                                                                                'mainListId',
                                                                                                                'name',
                                                                                                                'specs',
                                                                                                                'quantity',
                                                                                                                'unit',
                                                                                                                'price',
                                                                                                                'totalMoney',
                                                                                                                'remark',
                                                                                                                'delFlag',
                                                                                                                                                                                                                                                                                                                                                                            ])))
                })
            },
            save() {
                const {form: {validateFieldsAndScroll}} = this
                this.confirmLoading = true
                validateFieldsAndScroll((errors, values) => {
                    if (errors) {
                        this.confirmLoading = false
                        return
                    }
                    // 日期处理
                                        if (this.BaseTool.String.isBlank(values.id)) {
                        addPurchaseContractList(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    } else {
                        updatePurchaseContractList(values)
                                .then(() => {
                                    this.handleCancel(values)
                                }).catch(() => {
                            this.confirmLoading = false
                        })
                    }
                })
            },
            handleCancel(values) {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                if (this.BaseTool.Object.isNotBlank(values)) {
                    this.$emit('ok', values)
                }
            }

        }
    }
</script>
